package com.wanmait.travel.controller.user;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.travel.dao.ScenicDAO;
import com.wanmait.travel.dao.UserinfoDAO;
import com.wanmait.travel.util.ShopCar;
import com.wanmait.travel.vo.Scenic;
import com.wanmait.travel.vo.Userinfo;

@WebServlet("/UserinfoController")
public class UserinfoController  extends HttpServlet

{
	private UserinfoDAO userinfoDAO =UserinfoDAO.getDAO();
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{
		request.setCharacterEncoding("UTF-8");
		String action =request.getParameter("action");
		if(action==null) {
			return;
		}
		switch(action) {
		  case"reg":
			  this.reg(request,response);
			  break;
		  case"login":
			  this.login(request,response);
			  break;
		  case"show":
			  this.show(request, response);
			  break;
		  case"update":
			  this.update(request, response);
			  break;
		  case"updateshow":
			  this.updateshow(request, response);
			  break;
		  case"forgetpassword":
			  this.forgetpassword(request, response);
			  break;
		  case"changepassword":
			  this.changepassword(request, response);
			  break;

		  case"userinfo":
			  this.userinfo(request, response);
			  break;
		  case"pay":
			  this.pay(request, response);
			  break;

		  case"addCollection":
			  this.addCollection(request, response);
			  break;
		  case"deletedCollection":
			  this.deletedCollection(request, response);
			  break;
		  
			 
			  
		}
	}
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path=request.getServletContext().getContextPath();
		String userName =request.getParameter("userName");
		String userPass =request.getParameter("userPass");
		
		Userinfo userinfo = userinfoDAO.findByNameAndPass(userName, userPass);
		if(userinfo==null) {
			
			response.sendRedirect(path+"/userinfo/login.jsp?err=1");
		}
		else {
			
			HttpSession session = request.getSession();
			session.setAttribute("userinfo", userinfo);
			response.sendRedirect(path+"/userinfo/success.jsp");
		}
	}
	private void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		String path=request.getServletContext().getContextPath();
		String username =request.getParameter("userName");
		String userpass =request.getParameter("userPass");
		String nickname =request.getParameter("nickName");
		String gender =request.getParameter("gender");
		String phone =request.getParameter("phone");
		String pic =request.getParameter("pic");
		String birthdayStr =request.getParameter("birthday");
		String realName =request.getParameter("realName");
		String address =request.getParameter("address");
		Userinfo userinfo =new Userinfo();
		
		SimpleDateFormat format =new SimpleDateFormat("yyyy-mm-dd");
		Date birthday ;
		try {
			birthday= format.parse(birthdayStr);
			userinfo.setBirthday(birthday);
		} catch (java.text.ParseException e) 
		{
			e.printStackTrace();
		}
		userinfo.setUsername(username);
		userinfo.setUserpass(userpass);
		userinfo.setPic(pic);
		userinfo.setPhone(phone);
		userinfo.setNickname(nickname);
		userinfo.setGender(gender);
		userinfo.setRealName(realName);
		userinfo.setAddress(address);
		userinfoDAO.insert(userinfo);
		response.sendRedirect(path);
	}
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		
		
		String idstr=request.getParameter("id");
		int id=Integer.parseInt(idstr);
		Userinfo userinfo=userinfoDAO.findById(id);
		List<Scenic> scenics=userinfoDAO.findByCollection(id);

		request.setAttribute("userinfo", userinfo);
		request.setAttribute("scenics", scenics);
		request.getRequestDispatcher("/userinfo/my.jsp").forward(request, response);
	}
	private void updateshow(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		
		String idstr=request.getParameter("id");
		int id=Integer.parseInt(idstr);
		Userinfo userinfo=userinfoDAO.findById(id);
		request.setAttribute("userinfo", userinfo);
		request.getRequestDispatcher("/userinfo/userinfoupdate.jsp").forward(request, response);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{	
		
		String username =request.getParameter("username");
		String userpass =request.getParameter("userpass");
		String nickname =request.getParameter("nickname");
		String gender =request.getParameter("gender");
		String phone =request.getParameter("phone");
		String pic =request.getParameter("pic");
		String birthdayStr =request.getParameter("birthday");
		String realName =request.getParameter("real_name");
		String address =request.getParameter("address");
		String idstr=request.getParameter("id");
		int id=Integer.parseInt(idstr);

		Userinfo userinfo =new Userinfo();
		userinfo.setUsername(username);
		userinfo.setUserpass(userpass);
		userinfo.setPic(pic);
		userinfo.setPhone(phone);
		userinfo.setNickname(nickname);
		userinfo.setGender(gender);
		userinfo.setId(id);
		userinfo.setRealName(realName);
		userinfo.setAddress(address);
		
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		Date birthday = null;
		try {
			birthday=sdf.parse(birthdayStr);
			Timestamp pubDate=new Timestamp(birthday.getTime());
			userinfo.setBirthday(birthday);
		} catch (java.text.ParseException e) 
		{	
			e.printStackTrace();
		}
		List<Scenic> scenics=userinfoDAO.findByCollection(id);
		userinfoDAO.update(userinfo);
		request.setAttribute("userinfo", userinfo);
		request.setAttribute("scenics", scenics);
		request.getRequestDispatcher("/userinfo/my.jsp").forward(request, response);

	}

	private void forgetpassword(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{	
		
		String username=request.getParameter("username");
		String phone=request.getParameter("phone");
		String realName=request.getParameter("realname");
		Userinfo userinfo=userinfoDAO.findByName(username,phone,realName);
		
		
		if(userinfo==null)
		{
		    
			response.sendRedirect("/Travel/userinfo/forgetpassword.jsp?err=1");
			
		}
		else
		{
		Integer id=userinfo.getId();
		response.sendRedirect("/Travel/userinfo/changepassword.jsp?id="+id);
	}
	}
	

	
	protected void changepassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userpass=request.getParameter("userpass");
		String idstr =request.getParameter("id");
		int id=Integer.parseInt(idstr);
		userinfoDAO.updatePassword(id,userpass);
		response.sendRedirect("/Travel/userinfo/changesuccess.jsp");
		
	}

	
	protected void userinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Userinfo> userinfos=userinfoDAO.userinfoAll();
		request.setAttribute("userinfos", userinfos);
		request.getRequestDispatcher("/manager/userinfo.jsp").forward(request, response);
	}
	
	protected void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ShopCar shopCar=(ShopCar)request.getSession().getAttribute("shopCar");
		String userIdStr=request.getParameter("userId");
		int userId=Integer.parseInt(userIdStr);
		userinfoDAO.pay(shopCar,userId);
		response.getWriter().print(1);
	}
	

	protected void addCollection(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
		String scenicidstr = request.getParameter("scenicid");
		int scenicid = Integer.parseInt(scenicidstr); 
		int userinfoid = userinfo.getId();
		userinfoDAO.insertCollection(userinfoid, scenicid);
		}
	protected void deletedCollection(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idstr=request.getParameter("id");
		int id=Integer.parseInt(idstr);
		userinfoDAO.deletedCollection(id);
		}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		}
}
